package com.leetcode.partition12;

/**
 * @author `RKC`
 * @date 2021/12/21 9:55
 */
public class LC1154一年中的第几天 {

    private static int[] nums = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    private static int[] prefix = new int[13];

    static {
        for (int i = 1; i <= 12; i++) prefix[i] += prefix[i - 1] + nums[i - 1];
    }

    public static int dayOfYear(String d) {
        String[] s = d.split("-");
        int year = Integer.parseInt(s[0]), month = Integer.parseInt(s[1]), day = Integer.parseInt(s[2]);
        boolean isLeap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
        int answer = month > 2 && isLeap ? prefix[month - 1] + 1 : prefix[month - 1];
        return answer + day;
    }

    public static void main(String[] args) {
        System.out.println(dayOfYear("2019-01-09"));
        System.out.println(dayOfYear("2019-02-10"));
        System.out.println(dayOfYear("2003-03-01"));
        System.out.println(dayOfYear("2004-03-01"));
    }
}
